#include <iostream>
using namespace std;
//浅拷贝:简单的赋值拷贝操作:带来的问题就是堆区的内存重复释放，利用深拷贝解决
//深拷贝:在堆区重新申请空间，进行拷贝操作

class Person{
    public:
    //无参(默认)构造函数
        Person(){
            cout << "无参构造函数" << endl;
        }
        Person(int a,int height){
            m_Age = a;
            m_Height = new int(height);
            cout << "有参构造函数" << endl;
        }
        //自己实现拷贝构造函数 解决浅拷贝带来的问题平
        Person(const Person &p){
            cout << "拷贝构造函数调用" << endl;
            m_Age = p.m_Age;
            // m_Height = p.m_Height;编译器默认实现就是这行代码
            m_Height = new int(*p.m_Height);
        }
        ~Person(){
            //析构代码，堆区开辟数据做释放操作
            if(m_Height != NULL){
                delete m_Height;
                m_Height = NULL;
            }
            cout << "析构函数" << endl;
        }
        int m_Age;
        int *m_Height;
    };


void test01(){
    Person p1(18,160);
    cout << "p1的年龄为" << p1.m_Age << "身高为：" << *p1.m_Height << endl;
    Person p2(p1);
    cout << "p2的年龄为" << p2.m_Age << "身高为：" << *p2.m_Height << endl;

}
int main(){
	test01();
	system("pause");
	return 0;
}

